<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="电话"></el-input>
      </el-form-item>
      <el-form-item label="可用时长" prop="availableTime">
        <el-input v-model="dataForm.availableTime" placeholder="可用时长"></el-input>
      </el-form-item>
      <el-form-item label="充值次数" prop="rechargeNumber">
        <el-input v-model="dataForm.rechargeNumber" placeholder="充值次数"></el-input>
      </el-form-item>
      <el-form-item label="用户类型：0->学生;1->老师" prop="type">
        <el-input v-model="dataForm.type" placeholder="用户类型：0->学生;1->老师"></el-input>
      </el-form-item>
      <el-form-item label="用户状态：0->正常;1->禁用" prop="status">
        <el-input v-model="dataForm.status" placeholder="用户状态：0->正常;1->禁用"></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <el-input v-model="dataForm.avatar" placeholder="头像"></el-input>
      </el-form-item>
      <el-form-item label="imToken" prop="imToken">
        <el-input v-model="dataForm.imToken" placeholder="imToken"></el-input>
      </el-form-item>
      <el-form-item label="openId" prop="openId">
        <el-input v-model="dataForm.openId" placeholder="openId"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          createTime: '',
          deleted: '',
          version: '',
          lastModifyTime: '',
          name: '',
          phone: '',
          availableTime: '',
          rechargeNumber: '',
          type: '',
          status: '',
          avatar: '',
          imToken: '',
          openId: ''
        },
        dataRule: {
          name: [
            {required: true, message: '姓名不能为空', trigger: 'blur'}
          ], phone: [
            {required: true, message: '电话不能为空', trigger: 'blur'}
          ], availableTime: [
            {required: true, message: '可用时长不能为空', trigger: 'blur'}
          ], rechargeNumber: [
            {required: true, message: '充值次数不能为空', trigger: 'blur'}
          ], type: [
            {required: true, message: '用户类型：0->学生;1->老师不能为空', trigger: 'blur'}
          ], status: [
            {required: true, message: '用户状态：0->正常;1->禁用不能为空', trigger: 'blur'}
          ], avatar: [
            {required: true, message: '头像不能为空', trigger: 'blur'}
          ], imToken: [
            {required: true, message: 'imToken不能为空', trigger: 'blur'}
          ], openId: [
            {required: true, message: 'openId不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/manage/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 200) {
                Object.assign(this.dataForm, data.data)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/manage/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: `${!this.dataForm.id ? 'post' : 'put'}`,
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'phone': this.dataForm.phone,
                'availableTime': this.dataForm.availableTime,
                'rechargeNumber': this.dataForm.rechargeNumber,
                'type': this.dataForm.type,
                'status': this.dataForm.status,
                'avatar': this.dataForm.avatar,
                'imToken': this.dataForm.imToken,
                'openId': this.dataForm.openId
              })
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
